Central Article

Creating Offline Web Applications in Macromedia Central


Table of Contents

    1. Introduction
    2. How Central Uses Connection Awareness
    3. Caching Files and Data on the Client Machine
    4. Building a Central Application
    5. Implementing Network Status
    6. Adding Functionality with Application Controls
    7. Final Initiation
    8. Testing Your Central Application

Adding Functionality with Application Controls

Now you will add some functionality to UI elements, starting with the display function. You will use this functionality to display the image or file, or play the MP3 specified in the URL in the combo box.

// Display or play the specified file
function display(u) {
	
	if (!appShell.inLocalInternetCache(u) && !appShell.isConnected()) {
		u = u_pfx + "/noimage.gif";
	}
	/* Figure out what type of file we will be presenting and proceed accordingly */
	var ext = u.substr(u.lastIndexOf("."), u.length);
	if (ext == ".gif" || ext == ".jpg" || ext == ".swf") {
		_mc.loadMovie(u);
		_mc._visible = true;
		_txt._visible = false;
	} else if (ext == ".mp3") {
		_sound.loadSound(u, true);
		_mc._visible = false;
		_txt._visible = false;
	} else {
		var _lv = new LoadVars();
		_lv.onLoad = function(s) {
			delete this.onLoad;
			_txt.text = unescape(this);
		};
		_lv.load(u);
		_mc._visible = false;
		_txt._visible = true;
	}
}

The following functionality controls adding and removing files from cache. Initiate this functionality by clicking the add_btn icon button that you placed on the Stage earlier.

// Add or Remove items from the cache
add_btn.onRelease = function() {
	var u = url_cbx.getValue();
	
	/* If the specified file is currently in the cache, remove it from the cache as well as the LSO */
	if (appShell.inLocalInternetCache(u) && !inQueue(u)) {
		appShell.removeFromLocalInternetCache(u);
		var total = _dp.getLength();
		for (var i = 0; i < total; i++) {
			if (_dp.getItemAt(i).label == u) {
				_dp.removeItemAt(i);
			}
		}
	
	// Cache the specified file if it is not currently cached
	} else {
		
		/* If we have an internet connection, we can add the file to the cache right away; if not, we add the url of the file to the queue */
		if (appShell.isConnected()) {
			appShell.addToLocalInternetCache(u);
			_dp.addItem(u);
		} else {
			_que.addItem(u);
			
			/* Display a generic image identifying that the file is in the queue, waiting to be downloaded */
			display("noimage.gif");
		}
		
	}
};

The next three sections of code define the change handler of the URL combo box and change the UI based on the value in the combo box. It also adds functionality to the control button, which starts and stops a sound, so that it doesn't play all the way through. Lastly, the code adds functionality to determine if a file is in the queue, as in the onRelease function above.

// Define the url combo box change handler
url_cbx.setChangeHandler("url_change"); 
function url_change() {
	
	// Store the value of the url combo box
	var u = url_cbx.getValue();
	
	// Reset the icon of the add / remove button
	if (appShell.inLocalInternetCache(u)) {
		add_btn.setIcon("icon_bookmark_remove");
	} else {
		add_btn.setIcon("icon_bookmark_add");
	}
	
	// Display or play the currently selected item
	display(u);
}

// Sound control button
ctrl_btn.onRelease = function() {
	if (_sound.position) {
		_sound.stop();
	} else {
		_sound.start();
	}
};

// Check if file is in the queue
function inQueue(u) {
	var total = _que.getLength();
	for (var i = 0; i < total; i++) {
		if (_que.getItemAt(i).label == u) {
			return true;
		}
	}
}